package br.ufrgs.inf.sissw.app.helpers;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DbHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "infApp.db";
	private static final String DATABASE_TABLE = "contact";
	private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " ( _id INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ "name TEXT UNIQUE, mail TEXT, phone TEXT);";
	private static final String COLUMN_NAME = "name";
	private static final String COLUMN_MAIL = "mail";
	private static final String COLUMN_PHONE = "phone";

	private Context context;
	private SQLiteDatabase db;

	public DbHelper(Context context, SQLiteDatabase db) {
		super(context, DATABASE_NAME, null, 1);
		this.context = context;
		this.db = db;
	}

	public void insert(String _name, String _mail, String _phone) {
		try {

			if (!_name.contentEquals("")) {
				ContentValues cv = new ContentValues();
				cv.put(COLUMN_NAME, _name);
				cv.put(COLUMN_MAIL, _mail);
				cv.put(COLUMN_PHONE, _phone);
				db.insert(DATABASE_TABLE, null, cv);
			}
		} catch (Exception e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}
	}

	public void create() {
		try {
			db.execSQL(DATABASE_CREATE);
		} catch (SQLException e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}
	}

	/*
	 * public void count() { try { ArrayList<Contact> list = new
	 * ArrayList<Contact>(); String[] result_columns = new String[] {KEY_ID,
	 * COLUMN_NAME, COLUMN_MAIL, COLUMN_PHONE}; Cursor rows = db.query(true,
	 * DATABASE_TABLE, result_columns, null, null, null, null, null, null);
	 * 
	 * if(rows.moveToFirst()) { do { int id = rows.getInt(0); String name =
	 * rows.getString(1); String mail = rows.getString(2); String phone =
	 * rows.getString(3); Contact ct = new Contact(); ct.setName(name);
	 * ct.setMail(mail); ct.setPhone(phone); list.add(ct); }
	 * while(rows.moveToNext()); }
	 * 
	 * 
	 * CharSequence text = "Num of rows: " + list.size(); int duration =
	 * Toast.LENGTH_SHORT;
	 * 
	 * Toast toast = Toast.makeText(this.context, text, duration); toast.show();
	 * } catch (Exception e) { System.out.println("EXCEPTION: " +
	 * e.getMessage()); e.printStackTrace(); } }
	 */

	public void delete(String name) {
		try {
			if (!name.equals("")) {
				db.delete(DATABASE_TABLE, COLUMN_NAME + " = '" + name + "'",
						null);

				String text = "Deleted where name = " + name;
				Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
			}

		} catch (Exception e) {
			System.out.println("EXCEPTION: " + e.getMessage());
			e.printStackTrace();
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		create();
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		android.util.Log.w("Constants",
				"Upgrading database, which will destroy all old	data");
		db.execSQL("DROP TABLE IF EXISTS constants");
		onCreate(db);
	}
}
